package com.wpml.product.controller;

import com.wpml.common.util.PageUtils;
import com.wpml.common.util.ResultJson;
import com.wpml.product.entity.SpuInfoModel;
import com.wpml.product.service.SpuInfoService;
import com.wpml.product.vo.SpuSaveVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * spu信息
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-01 22:50:32
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {

    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:spuinfo:list")
    public ResultJson list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return ResultJson.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public ResultJson info(@PathVariable("id") Long id){
		SpuInfoModel spuInfo = spuInfoService.getById(id);

        return ResultJson.ok(spuInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public ResultJson save(@RequestBody SpuSaveVo vo){
		//spuInfoService.save(spuInfo);

        spuInfoService.saveSpuInfo(vo);

        return ResultJson.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public ResultJson update(@RequestBody SpuInfoModel spuInfo){
		spuInfoService.updateById(spuInfo);

        return ResultJson.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public ResultJson delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return ResultJson.ok();
    }

    @ApiOperation("上架商品")
    @PostMapping("/{spuId}/up")
    public ResultJson spuUp(@PathVariable Long spuId) {
        spuInfoService.up(spuId);
        return ResultJson.ok();
    }

}
